﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Customers
{
    /// <summary>
    /// This object represents the methods of a Customer.
    /// </summary>
    internal class CustomerDAO
    {

        #region general
        private static Customer LoadObj(IDataReader datareader)
        {
            Customer obj = new Customer();
            obj.CustomerID = MpDataHelp.GetInt(datareader, "CustomerID");
            obj.CustomerGUID = MpDataHelp.GetGuid(datareader, "CustomerGUID");
            obj.Email = MpDataHelp.GetString(datareader, "Email");
            obj.Username = MpDataHelp.GetString(datareader, "Username");
            obj.PasswordHash = MpDataHelp.GetString(datareader, "PasswordHash");
            obj.SaltKey = MpDataHelp.GetString(datareader, "SaltKey");
            obj.LanguageID = MpDataHelp.GetInt(datareader, "LanguageID");
            obj.IsAdmin = MpDataHelp.GetBoolean(datareader, "IsAdmin");
            obj.Active = MpDataHelp.GetBoolean(datareader, "Active");
            obj.Deleted = MpDataHelp.GetBoolean(datareader, "Deleted");
            obj.RegistrationDate = MpDataHelp.GetDateTime(datareader, "RegistrationDate");

            return obj;
        }
        #endregion

        public static int Customer_Add(Customer obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Customer_Add");
            db.AddOutParameter(dbCommand, "CustomerID", DbType.Int32, obj.CustomerID);
            db.AddInParameter(dbCommand, "CustomerGUID", DbType.Guid, obj.CustomerGUID);
            db.AddInParameter(dbCommand, "Username", DbType.String, obj.Username);
            db.AddInParameter(dbCommand, "Email", DbType.String, obj.Email);
            db.AddInParameter(dbCommand, "PasswordHash", DbType.String, obj.PasswordHash);
            db.AddInParameter(dbCommand, "SaltKey", DbType.String, obj.SaltKey);
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, obj.LanguageID);
            db.AddInParameter(dbCommand, "IsAdmin", DbType.Boolean, obj.IsAdmin);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, obj.Active);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, obj.Deleted);
            db.AddInParameter(dbCommand, "RegistrationDate", DbType.DateTime, obj.RegistrationDate);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@CustomerID"));
            return -1;
        }
        public static int Customer_Update(Customer obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Customer_Update");
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, obj.CustomerID);
            db.AddInParameter(dbCommand, "CustomerGUID", DbType.Guid, obj.CustomerGUID);
            db.AddInParameter(dbCommand, "Username", DbType.String, obj.Username);
            db.AddInParameter(dbCommand, "Email", DbType.String, obj.Email);
            db.AddInParameter(dbCommand, "PasswordHash", DbType.String, obj.PasswordHash);
            db.AddInParameter(dbCommand, "SaltKey", DbType.String, obj.SaltKey);
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, obj.LanguageID);
            db.AddInParameter(dbCommand, "IsAdmin", DbType.Boolean, obj.IsAdmin);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, obj.Active);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, obj.Deleted);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Customer_Delete(Int32 CustomerID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Customer_Delete");
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Customer> Customer_List(int CustomerID, string Username, string Email)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Customer_List");
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "Username", DbType.String, Username);
            db.AddInParameter(dbCommand, "Email", DbType.String, Email);
            List<Customer> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Customer>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}



